package org.bleep;

import org.bleep.util.*;
import org.bleep.things.*;
import org.bleep.physics.*;
import org.bleep.geom.*;
import org.bleep.input.*;

import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;

import org.lwjgl.opengl.GL11;

import java.util.ArrayList;
import java.util.Random;
import java.io.IOException;

/**
* <<Class summary>>
*
* @author   &lt;John Cromartie&gt;
* @version $Rev$
*/
public class Main extends Game implements ButtonEventListener, Interpolated {
	private SpriteSheet sprites;

	private Random r;
	
	private float x, y, rotation, width, height;

	public Main()
	{
		super();
	}

	public void init()
	{
		try
		{
			sprites = new SpriteSheet("org/bleep/resources/textures/sprites.png");
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		
		r = new Random();
		
		InputSystem.bind(new PushButton("jump", Keyboard.KEY_SPACE), this);
		InputSystem.bind(new HoldButton("move left", Keyboard.KEY_A), this);
		InputSystem.bind(new HoldButton("move right", Keyboard.KEY_D), this);
		InputSystem.bind(new PushButton("quit", Keyboard.KEY_ESCAPE), this);
		InputSystem.bind(new HoldButton("rotate left", Keyboard.KEY_Q), this);
		InputSystem.bind(new HoldButton("rotate right", Keyboard.KEY_E), this);
		
		x = y = 0f;
		rotation = 0f;
		width = 100f;
		height = 100;
	}

	public void render()
	{
		GL11.glPushMatrix();
		{
			float dx = width / 2f;
			float dy = height / 2f;
			
			GL11.glTranslatef(x, y, 0f);
			GL11.glPushMatrix();
			GL11.glTranslatef(dx, dy, 0f); // translate to center of object
			GL11.glRotatef(rotation, 0f, 0f, 1f); // rotate object
			GL11.glTranslatef(dx * -1f, dy * -1f, 0f);
			sprites.draw(new Rectangle(0f, 0f, width, height));
			GL11.glPopMatrix();
		}
		GL11.glPopMatrix();
	}

	public void handleButtonEvent(ButtonEvent b)
	{
		if (b.is("quit"))
		{
			quit();
		}
		
		if (b.is("move left"))
		{
			x -= 1;
		}
		
		if (b.is("move right"))
		{
			x += 1;
		}
		
		if (b.is("rotate left"))
		{
			rotation += 2f;
		}
		
		if (b.is("rotate right"))
		{
			rotation -= 2f;
		}
	}

	public void update(float timeStep)
	{
	}

	public static void main(String[] args)
	{
		Main g = new Main();
		g.run();
		System.exit(0);
	}
}